package com.flash.javamate;

import com.flash.javamate.entity.JavaFile;
import com.flash.javamate.utils.JavaFileParser;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by sunyiming on 2018/7/17.
 * Description:
 */
public class ApiDocDemo {

    public static File apisFile = new File("云端接口整理.csv");

    public static FileWriter writer;

    public static int apiCount = 0;
    public static int fileCount = 0;

    static {
        try {
            writer = new FileWriter(apisFile, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {

        CompilationUnit compilationUnit = JavaParser.parse(new File("E:\\projects\\iqiyiProjects\\moviebar-pay\\src\\main\\java\\com\\iqiyi\\moviebar\\pay\\controller\\MerchantRegisterController.java"));

        String userDir = "E:\\projects\\iqiyiProjects\\moviebar-cloud-api\\src\\main\\java\\";
        String classFullName = "com.iqiyi.moviebar.cloud.controller";

        String path = "E:\\projects\\iqiyiProjects\\moviebar-cloud-api\\moviebar-cloud\\src\\main\\java\\com\\iqiyi\\moviebar\\cloud\\controller";


        writer.append("包名，类名,方法名,接口地址").append("\r\n");

        func(new File(path));

        writer.flush();
        writer.close();


        System.out.println("总文件数：" + fileCount);
        System.out.println("总api类数：" + apiCount);
/*
        JavaFile javaFile = JavaFileParser.parse(userDir, classFullName);
        //System.out.println(javaFile);

        Map<String, String> fields = javaFile.getFields();
        for (String s : fields.keySet()) {
            System.out.println(s);
        }*/

        //HtmlGenerator.generate(javaFile);
    }

    private static void func(File file) throws IOException {
        File[] fs = file.listFiles();
        for(File f:fs){
            if(f.isDirectory()) {
                func(f);
            } else if(f.isFile()) {
                fileCount++;

                JavaFile javaFile = JavaFileParser.parse(f);
                Map<String, String> classAnn = javaFile.getAnnotations();
                if (!classAnn.containsKey("Controller")&&!classAnn.containsKey("RestController")){
                    System.out.println(javaFile.getClassPackage() + "----" + javaFile.getClassName());
                    continue;
                }
                String classMapping = checkAndGetMapping(classAnn);
                if (classMapping == null) {
                    classMapping = "";
                    //System.out.println(javaFile.getClassName());
                    //continue;
                }
                apiCount++;
                List<JavaFile.JavaMethod> methods = javaFile.getMethods();
                for (JavaFile.JavaMethod method : methods) {
                    Map<String, String> methodAnn = method.getAnnotations();
                    String methodMapping = checkAndGetMapping(methodAnn);
                    if (methodMapping == null) {
                        System.out.println(javaFile.getClassName() + "---" + method.getMethodName());
                        continue;
                    }
                    writer.append(String.format("%s,%s,%s,%s",
                            javaFile.getClassPackage(),
                            javaFile.getClassName().replaceAll(" extends BaseController",""),
                            method.getMethodName(),
                            (classMapping + methodMapping).replaceAll("value = ", ""))).append("\r\n");
                }
            }
        }
    }

    private static String checkAndGetMapping(Map<String, String> ann) {
        for (String s : ann.keySet()) {
            if (s.contains("Mapping")) {
                String value = ann.get(s);
                if (value==null){
                    return "";
                }
                return value;
            }
        }
        return null;
    }


}
